package com.zhang.hashtable;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;

/**
 * @author zhang
 * @time 2022/03/19 16:24:46
 */
public class HashTableExercise {
    public static int countPairsWithDiff() {
       int[] numbers = {1, 7, 5, 9, 2, 12, 3};
        HashSet<Integer> set = new HashSet<>();
        int count = 0;
        set.addAll(Arrays.asList(1, 7, 5, 9, 2, 12, 3));

        for (int number : numbers) {
            if (set.contains(number + 2)) {
                count++;
            }
            if (set.contains(number - 2)) {
                count++;
            }
            set.remove(number);
        }
        return count;


    }
    public static int[] twoSum() {
        int[] numbers = {1, 7, 5, 9, 2, 12, 3};
        HashMap<Integer, Integer> map = new HashMap<>();
        int target = 9;


        for (int i = 0; i < numbers.length; i++) {
            int complement = target - numbers[i];
            if (map.containsKey(complement)) {
                return new int[]{map.get(complement), i};
            }
            map.put(numbers[i], i);
        }

        return null;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(twoSum()));
    }

}
